TOOL.Category = "NPC Specifics"
TOOL.Name = "Dog"
TOOL.Command = nil
TOOL.ConfigName = ""

if CLIENT then
  language.Add( "Tool_npc_specific_dog_name", "NPC Specifics: Dog" )
  language.Add( "Tool_npc_specific_dog_desc", "Select and tell Dog to pick-up things" )
  language.Add( "Tool_npc_specific_dog_0", "Left click to select a Dog, Right-click to tell him to pickup a prop; Reload stops dog from throwing it again" )
end

local function npc_doNotify( message, symbol, user )
  if SERVER then
    user:SendLua("GAMEMODE:AddNotify(\"" .. message .. "\", " .. symbol .. ", \"5\"); surface.PlaySound( \"ambient/water/drip" .. math.random(1, 4) .. ".wav\" )")
  end
end

selected_dog = nil

function TOOL:LeftClick( trace )
  if (trace.Hit && !trace.HitWorld && !trace.HitSky) then
    //Is the entity nil?
    if (trace.Entity:IsValid()) then
      //Make sure it's an NPC
      if (trace.Entity:IsNPC() && trace.Entity:GetClass() == "npc_dog") then
        if (selected_dog == trace.Entity) then
          selected_dog = nil
          npc_doNotify("NPC un-selected", "NOTIFY_GENERIC", self:GetOwner())
        else
          selected_dog = trace.Entity
          npc_doNotify("NPC Selected", "NOTIFY_GENERIC", self:GetOwner())
        end
        return true
      end
    end
  end
  return false
end

function TOOL:RightClick( trace )
  if (trace.Entity:IsValid()) then
    if (!trace.Entity:IsNPC() && !trace.Entity:IsPlayer()) then
      if SERVER then
        trace.Entity:SetName(tostring(self:GetOwner():GetName()) .. "_dogthrow")
        if (selected_dog != nil && selected_dog:IsValid()) then
          selected_dog:Fire("StartCatchThrowBehavior", "", 0)
          selected_dog:Fire("SetPickupTarget", tostring(self:GetOwner():GetName()) .. "_dogthrow", 0)
          selected_dog:Fire("SetThrowArcModifier", 0.5, 0)
        end
        trace.Entity:SetName("")
      end
      return true
    end
  end
  return false
end

function TOOL:Reload( trace )
  if (selected_dog != nil && selected_dog:IsValid()) then
    if SERVER then selected_dog:Fire("StopCatchThrowBehavior", "", 0) end
    npc_doNotify("Dog will stop now", "NOTIFY_GENERIC", self:GetOwner())
  end
end
